const FromKey = 'fromPath';
const UserInfo = 'userInfo';

export function getToken(tokenKey) {
    return sessionStorage.getItem(tokenKey);
}

export function setToken(tokenKey, tokenVal) {
    return sessionStorage.setItem(tokenKey, tokenVal);
}
export function setBrand(brandKey, brandVal) {
    return sessionStorage.setItem(brandKey, brandVal);
}

export function removeToken(tokenKey) {
    return sessionStorage.removeItem(tokenKey);
}

export function setFromPath(path) {
    return sessionStorage.setItem(FromKey, path);
}

export function getFromPath() {
    return sessionStorage.getItem(FromKey);
}

export function getUserInfo() {
    return sessionStorage.getItem(UserInfo);
}

export function setUserInfo(userInfo) {
    return sessionStorage.setItem(UserInfo, userInfo);
}

export function removeUserInfo() {
    return sessionStorage.removeItem(UserInfo);
}

export function judgeAuth(limit, name) {
    if (typeof limit === 'string') {
        return limit.indexOf(name) > -1 || limit.indexOf('*') > -1;
    } else {
        return JSON.stringify(limit).indexOf(name) > -1 || JSON.stringify(limit).indexOf('*') > -1;
    }
}

export function setItem(name, value, store = sessionStorage) {
    store.setItem(name, JSON.stringify(value));
}

export function getItem(name, def, store = sessionStorage) {
    try {
        return JSON.parse(store.getItem(name)) || def;
    } catch (err) {
        return def;
    }
}

export function removeItem(name, store = sessionStorage) {
    store.removeItem(name);
}

export function clear(store = sessionStorage) {
    store.clear();
}
